package com.myapp.myhome.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RotateImage extends ImageView {

	private Bitmap mBitmap;
	private Paint mPaint;
	private Matrix mMatrix;
	private int mWidth;
	private int mHeight;
	/**
	 * 圆角的半径
	 */
	private int mRadius;
	/**
	 * 渲染图像，使用图像为绘制图形着色
	 */
	private BitmapShader mBitmapShader;

	public RotateImage(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mMatrix = new Matrix();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);

	}

	public RotateImage(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
		mMatrix = new Matrix();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
	}

	public RotateImage(Context context) {
		super(context, null);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
		mRadius = mWidth / 2;
		setMeasuredDimension(mWidth, mWidth);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		if (getDrawable() == null) {
			return;
		}
		setUpShader();
		canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);

	}

	private void setUpShader() {

		Drawable drawable = getDrawable();
		if (drawable == null) {
			return;
		}
		mBitmap = drawableToBitamp(drawable);
		mBitmapShader = new BitmapShader(mBitmap, TileMode.CLAMP,
				TileMode.CLAMP);
		float scale = 1.0f;

		// 拿到bitmap宽或高的小值
		int bSize = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
		scale = mWidth * 1.0f / bSize;

		// shader的变换矩阵，我们这里主要用于放大或者缩小
		mMatrix.setScale(scale, scale);
		// 设置变换矩阵
		mBitmapShader.setLocalMatrix(mMatrix);
		// 设置shader
		mPaint.setShader(mBitmapShader);
		updateShaderMatrix();
		invalidate();

	}

	private Bitmap drawableToBitamp(Drawable drawable) {
		if (drawable instanceof BitmapDrawable) {
			BitmapDrawable bd = (BitmapDrawable) drawable;
			return bd.getBitmap();
		}
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		drawable.draw(canvas);
		return bitmap;
	}

	private int currentRoatate = 0;

	private void updateShaderMatrix() {
		mMatrix.set(null);
		mMatrix.preRotate(currentRoatate, mWidth / 2, mWidth / 2);
		mBitmapShader.setLocalMatrix(mMatrix);

	}

//	public void setRoatate(int roateate) {
//		this.currentRoatate = roateate;
//		setUpShader();
//	}
//
//	public int getRoatate() {
//		return currentRoatate;
//	}
//
//	// 开始旋转
//		public void roatateStart() {
//			if (task != null) {
//				task.cancel(true);
//				task = null;
//			}
//			task = (CdTask) new CdTask().execute();
//
//		}
//
//		// 暂停
//		public void roatatePause() {
//			if (task != null) {
//				task.cancel(true);
//				task = null;
//			}
//		}
//
//		// 反转
//		public void reRotate() {
//			roatatePause();
//			isReversal = !isReversal;
//			roatateStart();
//		}
//
//		public void resetRotate(){
//			
//			if (task != null) {
//				task.cancel(true);
//				task = null;
//			}
//			currentRoatate = 0;
//			setUpShader();
//		}
//		
//		
//		
//		private CdTask task = null;
//
//		private boolean isReversal = false;
//
//		private class CdTask extends AsyncTask<Void, Integer, Void> {
//
//			@Override
//			protected Void doInBackground(Void... params) {
//				try {
//					while (true) {
//						if (isReversal) {
//							if (currentRoatate == 0) {
//								currentRoatate = 359;
//							}
//							currentRoatate--;
//						} else {
//							if (currentRoatate == 359) {
//								currentRoatate = 0;
//							}
//							currentRoatate++;
//						}
//						publishProgress(currentRoatate);
//						Thread.sleep(50);
//					}
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				return null;
//			}
//
//			@Override
//			protected void onProgressUpdate(Integer... values) {
//				setRoatate(values[0]);
//				super.onProgressUpdate(values);
//			}
//
//		}
}
